<%= lines(autogen_notice :go) -%>

package <%= package %>

import (
  "fmt"
  "testing"

  "github.com/hashicorp/terraform/helper/acctest"
  "github.com/hashicorp/terraform/helper/resource"
)
<%
   example = object.examples.select(&:is_basic).pop
   resource_name = product_ns + object.name + object.version.upcase
   test_case_name = resource_name + "_basic"
   terraform_name = "#{package}_" + Google::StringUtils.underscore(resource_name)
   terraform_resource_name = "#{terraform_name}.#{example.primary_resource_id}"

   read_api = object.read_api
   list_api = object.list_api
   sdkcp = argu_for_sdkclient(read_api || list_api, true)
%>

func TestAcc<%= test_case_name -%>(t *testing.T) {
    resource.Test(t, resource.TestCase{
        PreCheck:     func() { testAccPreCheck(t) },
        Providers:    testAccProviders,
        CheckDestroy: testAccCheck<%= "#{resource_name}" -%>Destroy,
        Steps: []resource.TestStep{
            {
                Config: testAcc<%= test_case_name -%>(acctest.RandString(10)),
                Check: resource.ComposeTestCheckFunc(
                    testAccCheck<%= "#{resource_name}" -%>Exists(),
                ),
            },
        },
    })
}

func testAcc<%= test_case_name -%>(val string) string {
<%= example.config_test product_folder -%>
}

func testAccCheck<%= resource_name -%>Destroy(s *terraform.State) error {
    config := testAccProvider.Meta().(*Config)
    client, err := config.sdkClient(<%= sdkcp %>)
    if err != nil {
        return fmt.Errorf("Error creating sdk client, err=%s", err)
    }

    for _, rs := range s.RootModule().Resources {
        if rs.Type != "<%= terraform_name -%>" {
            continue
        }
        <% if !read_api.nil? -%>

        url, err := replaceVarsForTest(rs, "<%= read_api.path -%>")
        if err != nil {
            return err
        }
        url = client.ServiceURL(url)

        _, err = client.Get(url, nil, &golangsdk.RequestOpts{
          <% if read_api.header_params.nil? -%>
            MoreHeaders: map[string]string{"Content-Type": "application/json"}})
          <% else -%>
            MoreHeaders: map[string]string{
                "Content-Type": "application/json",
            <% read_api.header_params.each do |k, v| -%>
                <%= sprintf("\"%s\": \"%s\",", k, v)%>
            <% end -%>
            }})
          <% end -%>
        if err == nil {
            return fmt.Errorf("<%= terraform_name -%> still exists at %s", url)
        }
        <% elsif !list_api.nil? -%>

        _, err = fetch<%= resource_name -%>ByListOnTest(rs, client)
        if err == nil {
            return fmt.Errorf("<%= terraform_name -%> still exists")
        }
        <% end -%>
    }

    return nil
}

func testAccCheck<%= resource_name -%>Exists() resource.TestCheckFunc {
    return func(s *terraform.State) error {
        config := testAccProvider.Meta().(*Config)
        client, err := config.sdkClient(<%= sdkcp %>)
        if err != nil {
            return fmt.Errorf("Error creating sdk client, err=%s", err)
        }

        rs, ok := s.RootModule().Resources["<%= terraform_resource_name -%>"]
        if !ok {
            return fmt.Errorf("Error checking <%= terraform_resource_name -%> exist, err=not found this resource")
        }
        <% if !read_api.nil? -%>

        url, err := replaceVarsForTest(rs, "<%= read_api.path -%>")
        if err != nil {
            return fmt.Errorf("Error checking <%= terraform_resource_name -%> exist, err=building url failed: %s", err)
        }
        url = client.ServiceURL(url)

        _, err = client.Get(url, nil, &golangsdk.RequestOpts{
          <% if read_api.header_params.nil? -%>
            MoreHeaders: map[string]string{"Content-Type": "application/json"}})
          <% else -%>
            MoreHeaders: map[string]string{
                "Content-Type": "application/json",
            <% read_api.header_params.each do |k, v| -%>
                <%= sprintf("\"%s\": \"%s\",", k, v)%>
            <% end -%>
            }})
          <% end -%>
        if err != nil {
            if _, ok := err.(golangsdk.ErrDefault404); ok{
                return fmt.Errorf("<%= terraform_resource_name -%> is not exist")
            }
            return fmt.Errorf("Error checking <%= terraform_resource_name -%> exist, err=send request failed: %s", err)
        }
        <% elsif !list_api.nil? -%>

        _, err = fetch<%= resource_name -%>ByListOnTest(rs, client)
        if err != nil {
            if strings.Index(err.Error(), "Error finding the resource by list api") != -1 {
                return fmt.Errorf("<%= terraform_name -%> is not exist")
            }
            return fmt.Errorf("Error checking <%= terraform_resource_name -%> exist, err=%s", err)
        }
        <% end -%>
        return nil
    }
}
<% if read_api.nil? and !list_api.nil? -%>

<%= lines(build_list_method_test(object, resource_name, list_api), 1) -%>
<% end -%>
